package com.education.web.api.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.education.web.common.core.base.BaseController;
import com.education.web.common.core.utils.ObjectUtils;
import com.education.web.api.admin.service.SystemAdminService;
import com.education.web.common.core.model.PageInfo;
import com.education.web.common.core.model.PageParam;
import com.education.web.common.core.utils.Result;
import com.education.web.dto.AdminRoleDto;
import com.education.web.dto.AdminUserSession;
import com.education.web.entity.SystemAdmin;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员管理接口
 * @author zengjintao
 * @version 1.0
 * @create_at 2019/12/17 19:33
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员管理接口")
public class SystemAdminController extends BaseController {

    @Autowired
    private SystemAdminService systemAdminService;

    /**
     * 根据name 查找用户
     * @param userName
     * @return
     */
    @GetMapping("selectByName")
    public Result<AdminUserSession> selectByName(String userName) {
        LambdaQueryWrapper queryWrapper = Wrappers.<SystemAdmin>query()
                .lambda().eq(SystemAdmin::getLoginName, userName);
        SystemAdmin systemAdmin = systemAdminService.getOne(queryWrapper);

        if (ObjectUtils.isEmpty(systemAdmin)) {
            return Result.fail();
        }
        AdminUserSession adminUserSession = systemAdminService.loadAdminPermission(systemAdmin);
        return Result.success(adminUserSession);
    }


    /**
     * 管理员列表
     * @param pageParam
     * @param systemAdmin
     * @return
     */
    @GetMapping
  //  @PreAuthorize("@authorityService.hasAuthority('system:admin:list')")
    public Result<PageInfo<SystemAdmin>> list(PageParam pageParam, SystemAdmin systemAdmin) {
        return Result.success(systemAdminService.listPage(pageParam, systemAdmin));
    }

    /**
     * 管理员详情
     * @param id
     * @return
     */
    @GetMapping("selectById")
    public Result selectById(Integer id) {
        return Result.success(systemAdminService.selectById(id));
    }

    /**
     * 添加或修改管理员
     * @param adminRoleDto
     * @return
     */
    @PostMapping
    public Result saveOrUpdate(@RequestBody AdminRoleDto adminRoleDto) {
        systemAdminService.saveOrUpdate(adminRoleDto);
        return Result.success();
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        return systemAdminService.deleteById(id);
    }


    /**
     * 修改密码
     * @param adminRoleDto
     * @return
     */
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody AdminRoleDto adminRoleDto) {
        systemAdminService.updatePassword(adminRoleDto);
        return Result.success("修改密码成功");
    }

    @GetMapping("test")
    public AdminUserSession test() {
        return new AdminUserSession(new SystemAdmin());
        /*Map resultMap = new HashMap<>();
        resultMap.put("code", "success");
        return resultMap;*/
    }
}
